﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using System.Reflection;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

namespace Dou.URP
{
    /// <summary>
    /// URP 工具类
    /// </summary>
    public static class URPUtil
    {
        private static ScriptableRendererData GetRenderData(string name)
        {
            var asset = GraphicsSettings.renderPipelineAsset;
            var propertyInfo = asset.GetType().GetField("m_RendererDataList", BindingFlags.Instance | BindingFlags.NonPublic);
            if (propertyInfo == null)
            {
                return null;
            }
            var rendererData = (ScriptableRendererData[]) propertyInfo.GetValue(asset);
            if (rendererData == null || rendererData.Length == 0)
            {
                return null;
            }
            foreach (var renderer in rendererData)
            {
                if (name.Equals(renderer.name))
                {
                    return renderer;
                }
            }
            return null;
        }
        
        private static ScriptableRendererFeature GetRendererFeature(ScriptableRendererData rendererData, string name)
        {
            foreach (var feature in rendererData.rendererFeatures)
            {
                if (name.Equals(feature.name))
                {
                    return feature;
                }
            }
            return null;
        }
        
        /// <summary>
        /// 获取指定 RenderData 下的所有 Feature
        /// </summary>
        public static List<ScriptableRendererFeature> GetAllFeatures(string name)
        {
            var renderData = GetRenderData(name);
            return renderData?.rendererFeatures;
        }
        
        /// <summary>
        /// 获取 Feature
        /// </summary>
        public static ScriptableRendererFeature GetFeature(string name, string featureName)
        {
            var rendererData = GetRenderData(name);
            return rendererData == null ? null : GetRendererFeature(rendererData, featureName);
        }
        
        /// <summary>
        /// 设置 Feature 是否激活
        /// </summary>
        public static void SetFeatureActive(string name, string featureName, bool active)
        {
            var feature = GetFeature(name, featureName);
            feature?.SetActive(active);
        }
        
        /// <summary>
        /// 是否开启摄像机深度图
        /// </summary>
        public static void SetDepthTexture(bool support)
        {
            var pipeline = (UniversalRenderPipelineAsset) GraphicsSettings.currentRenderPipeline;
            pipeline.supportsCameraDepthTexture = support;
        }
        
        /// <summary>
        /// 设置 MSAA 采样数量
        /// </summary>
        public static void SetMSAA(int msaaSampleCount)
        {
            var pipeline = (UniversalRenderPipelineAsset) GraphicsSettings.currentRenderPipeline;
            pipeline.msaaSampleCount = msaaSampleCount;
        }
    }
}
